package com.feng.timu.controller;

import com.feng.common.dto.PageDto;
import com.feng.common.utils.UploadUtil;
import com.feng.common.vo.ApiRest;
import com.feng.common.vo.PageResult;
import com.feng.tiku.entity.Repo;
import com.feng.tiku.service.RepoService;
import com.feng.timu.dto.QuDto;
import com.feng.timu.dto.QuPagingDto;
import com.feng.timu.entity.Qu;
import com.feng.timu.service.QuService;
import com.feng.timu.vo.QuVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 问题题目 前端控制器
 * </p>
 *
 * @author feng
 * @since 2023-03-21 15:34:38
 */
@RestController
@RequestMapping("/timu/qu")
@CrossOrigin
@Api(tags = "题目模块")
public class QuController {

    @Autowired
    private RepoService repoService;

    @Autowired
    private QuService quService;

    @PostMapping("upload/img")
    @ApiOperation("题目图片上传")
    public ApiRest upload(MultipartFile file) {
        return UploadUtil.uploadFile(file);
    }

    @GetMapping("repos")
    @ApiOperation("所有题库查询")
    public ApiRest listRepo() {
        List<Repo> list = repoService.list();
        return ApiRest.success("查询成功", list);
    }

    @PostMapping
    @ApiOperation("添加题目")
    public ApiRest insertQu(@RequestBody QuDto quDto) {
        return quService.insertQu(quDto);
    }

    @PostMapping("page")
    @ApiOperation("题目分页查询")
    public PageResult<Qu> pagingQu(@RequestBody PageDto<QuPagingDto> pageDto) {
        return quService.pagingQu(pageDto);
    }

    @GetMapping("{quId}")
    @ApiOperation("查询单个题目")
    public ApiRest<QuVO> findQu(@PathVariable String quId) {
        return quService.findQu(quId);
    }

    @PostMapping("edit")
    @ApiOperation("修改题目")
    public ApiRest updateQu(@RequestBody QuDto quDto) {
        return quService.updateQu(quDto);
    }

    @Transactional
    @DeleteMapping("{quId}")
    @ApiOperation("逻辑删除")
    public ApiRest deleteById(@PathVariable String quId) {
        return quService.removeById(quId) ?
                ApiRest.success("删除成功", null) :
                ApiRest.success("删除失败", null);
    }

    @Transactional
    @DeleteMapping("/more/{quIds}")
    @ApiOperation("逻辑删除批删")
    public ApiRest deleteByIds(@PathVariable String[] quIds) {
        return quService.removeBatchByIds(Arrays.asList(quIds), true) ?
                ApiRest.success("删除成功", null) :
                ApiRest.success("删除失败", null);
    }
}
